<?php session_start();//UTF-8无BOM就是为了处理这个问题  
	include_once('./public/debugSetting.php');
	include_once("logAndVer.php");//登录检测
	include_once('./public/common.php');
	if($retFlag == false){
		echo "404";
		return false;
	}
?>
<!DOCTYPE html>
<html lang="zh-CN"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
	<?php //TODO:change icon ?>
    <link rel="icon" href="./public/pic/pHctrlico.ico">

    <title>device Board</title>

    <!-- Bootstrap core CSS -->
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="../bootstrap/dashboard.css" rel="stylesheet">

  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">pHctrl</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
		<?php //TODO:?? ?>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">DeviceBoard</a></li>
          <!--  <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li>
			-->
          </ul>
          <form class="navbar-form navbar-right">
           <!-- <input class="form-control" placeholder="Search..." type="text"> -->
          </form>
        </div>
      </div>
    </nav>
	
    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Overview <?php echo '('.$uid.')';?><span class="sr-only">(current)</span></a></li>
			<?php
				
				if($level && intval($level) >= 1){
					$spesa = specialEncode($salt,$constSalt);
					$speu = specialEncode($uid,$constSalt);
					echo '<li><a href="./userManage.php?spesa='.urlencode($spesa).'&speu='.urlencode($speu).'">User</a></li>';
					
				}
				//change passwd: use new verification
				$newVer = randStr(rand(1,6));
				$verNewVer = specialEncode($newVer,"");
				$newUid = specialEncode($uid,$newVer);
				echo '<li><a href="./changePwd.php?newVer='.urlencode($newVer).'&verNewVer='.urlencode($verNewVer).'&newUid='.urlencode($newUid).'">change password</a></li>';
				
			?>
			<?php //如果权限满足条件(>=1)则允许添加传感器
				
				if($level && intval($level) >= 1){
					$spesa = specialEncode($salt,$constSalt);
					$speu = specialEncode($uid,$constSalt);
					echo '<li><a href="./deviceManage.php?spesa='.urlencode($spesa).'&speu='.urlencode($speu).'">Device</a></li>';
				}
						
			?>
            <li><a href="#Sensor_Data">SensorData</a></li>
            <?php
				//>=2 : read user info
				if($level && intval($level) >= 2){
					echo '<li><a href="#User_Info">UserInfo</a></li>';
					echo '<li><a href="#Spec_Func">SpecFunction</a></li>';
				}
			
			?>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="../index.php">Log out</a></li>
           
          </ul>
          
        </div>
	
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header" ><a name="Device_board">Device board</a></h1>

          <div class="row placeholders" >
            
				<?php
				    include_once("./public/opDB.php");
					//display the device
					$sql_dev = 'select devName,devType,devPic from device,user where device.uid=user.uid and user.uid='.$uid.' limit 0,30';
					$res = mysql_query($sql_dev) or die('fail to query the device');
					$i = 0;
					while($row = mysql_fetch_array($res)){
						++$i;
						$devName = $row['devName'];
						$devType = $row['devType'];
						$devPic = $row['devPic'];
						echo '<div class="col-xs-6 col-sm-3 placeholder">
						<img data-holder-rendered="true" src=';
						if($devPic == NULL){
							echo '"" data-src="holder.js/200x200/auto/'.($i%2==0?'sky':'vine');
							
						}else{
							echo '"'.$devPic;
							
						}				
						echo '" class="img-responsive" alt="200x200">';
						echo '<h4>'.$devName.'</h4>
							  <span class="text-muted">'.$devType.'</span>
							</div>			
							';
					}
					
					//not close for we need it after
					//mysql_close($con);
				
				?>
            
            
          </div>

          <h2 class="sub-header"><a name="Sensor_Data">Sensor Data</a></h2>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>device Id</th>
                  <th>sensor Id</th>
                  <th>start time</th>
                <!--  <th>Header</th>
                  <th>Header</th>
				  -->
                </tr>
              </thead>
			  
              <tbody>
				<?php
					
					$saltlen = mt_rand(5,11);
					$salt = '';
					for($i=0;$i<$saltlen;++$i){
						$salt .= chr(ord('A')+mt_rand(0,25));
					}
					if(empty($uid)){
						echo alertHtmlString("404");
						return false;
					}
					if($verifyFlag){
						include_once("./public/opDB.php");
					}
					//display current user's device
					$row = mysql_query("select `device`.`did`,`sensordata`.`sid`,`sensordata`.`start` from `device`,`sensordata` where `device`.`uid`=".$uid." and `device`.`did`=`sensordata`.`did` order by `device`.`did` limit 0,20");
					
					
					
					while($res = mysql_fetch_array($row)){
						echo "<tr>";
						$sid = $res['sid'];
						$did = $res['did'];
						$startTime = $res['start'];
						$spe= specialEncode($sid,$salt);
						$spesa = specialEncode($salt,$constSalt);
						$speu = specialEncode($uid,$constSalt);
						echo '<td><a href="show.php?spe='.urlencode($spe).'&spesa='.urlencode($spesa).'&pms='.urlencode($postMD5SessionId).'&speu='.urlencode($speu).'">'.$did.'</a></td>';
						echo '<td><a href="show.php?spe='.urlencode($spe).'&spesa='.urlencode($spesa).'&pms='.urlencode($postMD5SessionId).'&speu='.urlencode($speu).'">'.$sid.'</a></td>';
						echo '<td><a href="show.php?spe='.urlencode($spe).'&spesa='.urlencode($spesa).'&pms='.urlencode($postMD5SessionId).'&speu='.urlencode($speu).'">'.$startTime.'</a></td>';
						/* echo '
						  <td>h</td>
						  <td>h</td>';
						  */
						echo "</tr>";
					}
					
					if($level < 2)
						mysql_close($con);
					
				?>
				
              </tbody>
            </table>
          </div>
        
     
	 
	    <?php
			//admin : read user info
			if($level >= 2){
				echo '
				<h2 class="sub-header"><a name="User_Info">User Info</a></h2>
				  <div class="table-responsive">
					<table class="table table-striped">
					  <thead>
						<tr>
						  <th>user Id</th>
						  <th>hauser</th>
						  <th>level</th>

						</tr>
					  </thead>
					  
					  <tbody>';
				$sql_user = 'select uid,hauser,level from user order by uid limit 0,30';
				$res = mysql_query($sql_user) or die('fail to find user info');
				
				while($row=mysql_fetch_array($res)){
					$q_uid = $row['uid'];
					$q_hauser = $row['hauser'];
					$q_level = $row['level'];
					
					echo '<tr>';
					echo '<td>';
					echo $q_uid;
					echo '</td>';
					echo '<td>';
					echo $q_hauser;
					echo '</td>';
					echo '<td>';
					echo $q_level;
					echo '</td>';
					
					echo '</tr>';
					
				}

				echo '</tbody>
				   </table>';
				
			
		  
			  //special select function
				echo '<h2 class="sub-header"><a name="Spec_Func">Special Func</a></h2>';
				
				$specFuncNum = 5;
				$styleNum = 4;
				
				$urlHeader = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
				$urlHeader = $urlHeader.'?specFunc=';
				for($i=1;$i<=$specFuncNum;++$i){
					echo '<button type="button" ';
					if(($i % $styleNum) == 0)
						echo 'class="btn btn-lg btn-warning"><a href="./index.php?specFunc='.$i.'">';
					else if(($i % $styleNum) == 1)
						echo 'class="btn btn-lg btn-default"><a href="./index.php?specFunc='.$i.'">';
					else if(($i % $styleNum) == 2)
						echo 'class="btn btn-lg btn-success"><a href="./index.php?specFunc='.$i.'">';
					else
						echo 'class="btn btn-lg btn-info"><a href="./index.php?specFunc='.$i.'">';
					echo $i.'</a></button>  ';
				}
				
				$specFuncNo = getGetPara("specFunc");
				
				//echo "specFuncNo:".$specFuncNo;
				if($specFuncNo == NULL || intval($specFuncNo) > $specFuncNum || intval($specFuncNo) < 0){
					$specFuncNo = 0;//do not do the special query
				}else{
					$specFuncNo = intval($specFuncNo);
				}
				
				$funcResTitle = NULL;
				$res = NULL;
				$sqlSFN  = NULL;
				$descrpt = NULL;
				switch($specFuncNo){
				case 1:
					//show the device number of each user DESC
					$descrpt = "the device number of each user DESC";
					$funcResTitle = array('uid','deviceCnt');
					$sqlSFN = 'SELECT uid , count( did ) deviceCnt from device group by uid order by deviceCnt DESC LIMIT 0, 30 ';
					
					//echo "specFuncNo:".$specFuncNo;
					break;
				case 2:
					//count the sensorData number of each user DESC
					$descrpt = "the sensorData number of each user DESC";
					$funcResTitle = array('uid','dataCnt');
					$sqlSFN = 'select device.uid , count( sensordata.sid ) dataCnt from device , sensordata where device.did = sensordata.did group by device.uid order by dataCnt DESC LIMIT 0, 30 ';
					
					break;
				case 3:
				
					//the number of the device which belongs to  the same user is unique
					$descrpt = "the number of the device which belongs to  the same user is unique";
					$funcResTitle = array('uid','deviceCnt');
					$sqlSFN = 'select uid, deviceCnt from user_deviceCnt group by deviceCnt having count( * ) = 1 LIMIT 0, 30 ';
					
					//the view user_deviceCnt: uid,deviceCnt already created
					
					break;				
				case 4:
					//show uid,did,hauser,hadevice for sending data 
					$descrpt = 'show uid,did,hauser,hadevice for sending data ';
					$funcResTitle = array('uid','did','hauser','hadevice');
					$sqlSFN = 'select device.uid , device.did , hauser , hadevice from user , device where user.uid = device.uid LIMIT 0, 30 ';
					break;
				case 5:
					//in the recent week ,who post the most requests
					$descrpt = "in the recent week ,who post the most requests";
					$funcResTitle = array('uid','dataCnt');
					$sqlSFN = 'select device.uid , count( sensordata.sid ) dataCnt from device , sensordata where device.did = sensordata.did and UNIX_TIMESTAMP( '
        . ' sensordata.START ) >= UNIX_TIMESTAMP( ) -7 *24 *3600'
        . ' AND UNIX_TIMESTAMP( '
        . ' START ) <= UNIX_TIMESTAMP( ) group by device.uid order by dataCnt DESC limit 0,1';;
					
					break;				
				
				}
				
				echo '</div>';
				
				echo '
					<h2 class="sub-header"><a name="Spec_Func">Special Query Result</a><p></p>';
				echo $descrpt.'</h2>
					  <div class="table-responsive">
						<table class="table table-striped">
						  <thead>
							<tr>';
				
				
				if($sqlSFN != NULL){
					//do not show wrong answer
					$res = mysql_query($sqlSFN) or die('');
					for($i=0;$i<count($funcResTitle);++$i){
						echo '<th>'.$funcResTitle[$i].'</th>';
					}
					echo '      </tr>
							  </thead>
						  ';
					echo  '<tbody>';
					
					
					
					//echo "frt:$funcResTitle[0]\n";	
					
					while($row = mysql_fetch_array($res)){
						echo '<tr>';
						for($i=0;$i<count($funcResTitle);++$i){
							
							echo '<td>'; 
							echo $row[$funcResTitle[$i]];
							echo '</td>';
							
						}
						echo '</tr>';
						
					}
					echo '</tbody>
						   </table>';
				}
				echo '</div>';
				
				echo '</div>';
				
				

				if($con)
					mysql_close($con);        
			}//end if($level >= 2)
		?>
			
	
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="../bootstrap/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.js"></script>
    <!-- Just to make our placeholder images work. Don't actually copy the next line! -->
    <script src="../bootstrap/holder.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../bootstrap/ie10-viewport-bug-workaround.js"></script>
  

<svg style="visibility: hidden; position: absolute; top: -100%; left: -100%;" preserveAspectRatio="none" viewBox="0 0 200 200" height="200" width="200"><defs></defs><text style="font-weight:bold;font-size:10pt;font-family:Arial, Helvetica, Open Sans, sans-serif;dominant-baseline:middle" y="10" x="0">200x200</text></svg>

</body></html>
